package com.bawei.bydayzg_two.viewModel

import androidx.lifecycle.viewModelScope
import com.bawei.base.bean.CartResult
import com.bawei.base.bean.PreOrderResult
import com.bawei.base.bean.Res
import com.bawei.base.http.SingleLiveData
import com.bawei.base.mvvm.BaseViewModel
import com.bawei.base.param.PPreOrder
import kotlinx.coroutines.launch

class CartViewModel:BaseViewModel() {
    //请求购物车列表
    val cartSuccess = SingleLiveData<Res<CartResult>>()
    val cartFail= SingleLiveData<String>()
    //
    fun cart(isValid:String,limit:String,page:String){
        viewModelScope.launch {
            try {
                cartSuccess.postValue(apiService.cart(isValid, limit, page))
            } catch (e: Exception) {
                cartFail.postValue(e.message.toString())
            }
        }
    }

    //
    val cartUpdateSuccess = SingleLiveData<Res<String?>>()
    val cartUpdateFail = SingleLiveData<String>()
    //
    fun cartUpdate(id:Int,number:Int){
        viewModelScope.launch {
            try {
                cartUpdateSuccess.postValue(apiService.cartUpdate(id, number))
            } catch (e: Exception) {
                cartUpdateFail.postValue(e.message.toString())
            }
        }
    }

    //
    val cartDeleteSuccess = SingleLiveData<Res<String?>>()
    val cartDeleteFail = SingleLiveData<String>()
    //
    fun cartDelete(ids:Array<Int>){
        viewModelScope.launch {
            try {
                cartDeleteSuccess.postValue(apiService.cartDelete(ids))
            } catch (e: Exception) {
                cartDeleteFail.postValue(e.message.toString())
            }
        }
    }

    //提交订单，预下单
    val preOrderSuccess = SingleLiveData<Res<PreOrderResult>>()
    val preOrderFail = SingleLiveData<String>()
    //
    fun preOrder(pPreOrder: PPreOrder){
        viewModelScope.launch {
            try {
                preOrderSuccess.postValue(apiService.preOrder(pPreOrder))
            } catch (e: Exception) {
                preOrderFail.postValue(e.message.toString())
            }
        }
    }
}